/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package MainClasses;

import java.io.*;
import java.util.*;
/**
 *
 * @author andrewcutler
 */
public class Preferences {

    private boolean runBefore;
    private int studentID;
    private String studentEmail;
    private String studentName;
    private CourseManager manager;
    
    public Preferences(CourseManager m)
    {
        studentEmail = "";
        manager = m;
        runBefore = false;
    }
    
    public CourseManager getCourseManager()
    {
        return manager;
    
    }
    
    public void setStudentName(String s)
    {
        studentName = s;
    }
    
    public String getStudentName()
    {
        return studentName;
    }
    
    public int getID()
    {
        return studentID;
    }
    public void setID(int i)
    {
        studentID = i;
    }
    public void setEmail(String s)
    {
        studentEmail = s;
    }
    public String getEmail()
    {
        return studentEmail;
    }
    public boolean runBefore()
    {
        return runBefore;
    }
    public void setRunBefore(boolean b)
    {
        runBefore = b;
    }
    public String getFileName(int num)
    {
        return fileName("Data"+num);
    }
    
    public String getToDoName()
    {
        return fileName("ToDo");
    }
    
    public String getVersionNumber()
    {
        return "0.04b";
    }
    public String getCheckedName()
    {
        return fileName("Checks");
    }
    
    public String getUserFileName()
    {
        return fileName("Events");
    }
    public String getPrefsName()
    {
        return "prefs.hm";
    }
    
    private String fileName(String end)
    {
        return filePath() + studentID + "\'s" + end + ".hm";
    }
    private String filePath()
    {
        return "." + File.separator + studentID + "\'sFolder" + File.separator;
    }
    
    public void makeDirectories()
    {
        File f = new File(filePath());
        if(!f.exists())
            f.mkdir();
    }
    
    public boolean loadPreferences()
    {
        DataInputStream in;
        
        try
        {
            File file = new File(getPrefsName());
            in = new DataInputStream(new FileInputStream(file));
            String currentLine;
            
            while(in.available() != 0)
            {
                currentLine = in.readLine();
                
                if(currentLine.contains("ID:"))
                {
                    studentID = Integer.parseInt(currentLine.substring(3));
                }
                else if(currentLine.contains("RUN:"))
                {
                    runBefore = Boolean.parseBoolean(currentLine.substring(4));
                }
                else if(currentLine.contains("EMAIL:"))
                {
                    studentEmail = currentLine.substring(6);
                }
            }
            
        }
        catch(IOException e)
        {
            runBefore = false;
        }
        
        return runBefore;
    }
    public void savePreferences()
    {
        PrintWriter out;
        
        try
        {
            File file = new File(getPrefsName());
            makeDirectories();
            out = new PrintWriter(new FileWriter(file));
            out.println("RUN:true");
            out.println("ID:"+studentID);
            out.println("EMAIL:"+studentEmail);
            out.close();
            
        }
        catch(IOException e)
        {
            System.out.println("ERROR: " + e);
        }
        
    }
}
